package xyz.repo;

import android.app.Application;
import android.os.AsyncTask;
import android.widget.Toast;


import androidx.lifecycle.LiveData;

import java.util.List;

import xyz.dao.NoteDao;
import xyz.dao.NoteDatabase;

import xyz.entity.Note;
import xyz.test.EditNoteActivity;

public class NoteRepo {

    private NoteDao mNoteDao;
    private Application mApp;

    public NoteRepo(Application application) {
        mNoteDao = NoteDatabase.getInstance(application).getNoteDao();
        mApp = application;
    }

    public LiveData<List<Note>> getAllNote() {
        return mNoteDao.getAllNote();
    }

    public LiveData<Note> getNoteById(int id) {
        return mNoteDao.getLiveNote(id);
    }

    public void addNote(Note note) {
        new AddNoteTask(mNoteDao, mApp).execute(note);
    }

    public void updateNote(Note note, EditNoteActivity.OnUpdatedListener lister) {
        new UpdateNoteTask(mNoteDao, lister).execute(note);
    }
    public void deleteNote(Note note) {
        new DeleteNoteTask(mNoteDao, mApp).execute(note);
    }
    private static class AddNoteTask extends AsyncTask<Note, Void, Boolean> {
        private NoteDao mNoteDao;
        private Application mApp;

        AddNoteTask(NoteDao noteDao, Application app) {
            mNoteDao = noteDao;
            mApp = app;
        }

        @Override
        protected Boolean doInBackground(Note... notes) {
            return mNoteDao.insert(notes[0]) > 0;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);

            if (aBoolean) {
                Toast.makeText(mApp, "成功保存文件", Toast.LENGTH_LONG);
            } else {
                Toast.makeText(mApp, "出错了", Toast.LENGTH_LONG);
            }
        }

    }

    private static class UpdateNoteTask extends AsyncTask<Note, Void, Boolean> {
        private NoteDao mNoteDao;
        private EditNoteActivity.OnUpdatedListener mListener;

        UpdateNoteTask(NoteDao noteDao, EditNoteActivity.OnUpdatedListener listener) {
            mNoteDao = noteDao;
            mListener = listener;
        }

        @Override
        protected Boolean doInBackground(Note... notes) {

            return mNoteDao.updateNote(notes[0]) > 0;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);

            if (aBoolean) {
                mListener.OnUpdated();
            } else {

            }
        }

    }


    private static class DeleteNoteTask extends AsyncTask<Note, Void, Boolean> {
        private NoteDao mNoteDao;
        private Application mApp;

        DeleteNoteTask(NoteDao noteDao, Application app) {
            mNoteDao = noteDao;
            mApp = app;
        }

        @Override
        protected Boolean doInBackground(Note... notes) {
            return mNoteDao.delete(notes[0]) > 0;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);

            if (aBoolean) {
                Toast.makeText(mApp, "删除成功", Toast.LENGTH_LONG);
            } else {
                Toast.makeText(mApp, "出错了", Toast.LENGTH_LONG);
            }
        }

    }
}
